package webservices;

import java.util.List;
import dao.DAOFactory;

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET; 
import javax.ws.rs.HeaderParam;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path; 
import javax.ws.rs.PathParam; 
import javax.ws.rs.Produces; 
import javax.ws.rs.core.Context;
import javax.ws.rs.core.GenericEntity;
import javax.ws.rs.core.MediaType; 
import javax.ws.rs.core.Request;
import javax.ws.rs.core.UriInfo;

import sun.security.krb5.Credentials;
import classes.Market;
import classes.Poslovnica;
import classes.Proizvod;

@Path("poslovnice/")
public class PoslovnicaResource {

	@Context
	 UriInfo uriInfo;
	 @Context
	 Request request;
	 
	/** 
	 @GET
	 @Path("login")
	 @Produces(MediaType.APPLICATION_XML)
	 public Market prijaviNaSistem(@HeaderParam("username")String username, @HeaderParam("password")String password) throws Exception{
		 try{
			 DAOFactory.connect();
			 String naziv = MarketDAO.nazivMarketa(username)
			 DAOFactory.disconnect();
			 return k;
		 }
		 catch(Exception e){
			 e.printStackTrace();
			 return null;
		 }
	  }*/
	 
	 @Path("/svi")
		@GET
		@Produces(MediaType.APPLICATION_JSON)
		public GenericEntity<List<Poslovnica>> vratiPoslovnice() {
			try {
				DAOFactory.connect();
				List<Poslovnica> poslovnice = DAOFactory.getPoslovnicaDAO().selectSvePoslovnice();
				DAOFactory.disconnect();
				return new GenericEntity<List<Poslovnica>>(poslovnice){};
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return null;
			}
		}
	
	 @Path("/poslovnica/{id}")
		@GET
		@Produces(MediaType.APPLICATION_XML)
		public Poslovnica vratiPoslovnicu(@PathParam("id")String sIDPoslovnica) {
			try {
				int IDPoslovnica = Integer.parseInt(sIDPoslovnica);
				DAOFactory.connect();
				Poslovnica p = DAOFactory.getPoslovnicaDAO().selectPoslovnica(IDPoslovnica);
				DAOFactory.disconnect();
				return p;
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return null;
			}
		}
	 
	 @PUT
	  @Path("poslovnica/novi")
	  @Consumes(MediaType.APPLICATION_XML)
	  @Produces(MediaType.APPLICATION_XML)
	  public String putPoslovnica(@HeaderParam("username")String username, @HeaderParam("password")String password, Poslovnica p) throws Exception {
		  try{
			  DAOFactory.connect();
			  DAOFactory.getPoslovnicaDAO().insertPoslovnica(p);
			  DAOFactory.disconnect();
			  return Boolean.toString(true);
		  }
		  catch(Exception e){
			  e.printStackTrace();
			  return Boolean.toString(false);
		  }
	  }
	 
	 
	 @POST
	  @Path("poslovnica/obrisi")
	  @Consumes(MediaType.APPLICATION_XML)
	  @Produces(MediaType.APPLICATION_XML)
	  public String deletePoslovnicaPOST(@HeaderParam("username")String username, @HeaderParam("password")String password, String sIDPoslovnica){
		  try{
			  int IDPoslovnica = Integer.parseInt(sIDPoslovnica);
			  DAOFactory.connect();
			  int i = DAOFactory.getPoslovnicaDAO().deletePoslovnica(IDPoslovnica);
			  if (i!=1){
				  DAOFactory.disconnect();
				  return Boolean.toString(false);
			  }
			  DAOFactory.disconnect();
			  return Boolean.toString(true);
		  }
		  catch(Exception e){
			  e.printStackTrace();
			  return Boolean.toString(false);
		  }
	  }
	
}
